/*
 * This program is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, or 
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License along 
 * with this program; if not, write to the Free Software Foundation, Inc., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <QFile>
#include <QFileInfo>
#include <QSettings>
#include <QRegExp>
#include "wakabaagent.h"
#include "log.h"
#include "components/chan.h"
#include "components/board.h"
#include "captchafetchers/captchafetcher.h"
#include "wakabatasksendpost.h"

namespace Wishmaster
{
	WakabaAgent::WakabaAgent(const QString& host) : DefaultAgent(host),
		m_host(host)
	{
	}

	WakabaAgent::~WakabaAgent()
	{
	}

	void WakabaAgent::makePost(const PostDescriptor& post, const QString& captcha)
	{
		TaskPtr task = TaskPtr(new WakabaTaskSendPost(post, captcha, requestManager(), "Sending post", this));
		m_postRequests[task->id()] = post.parent_thread;

		connect(task.data(), SIGNAL(result(TaskSendPost*,QByteArray,QString)), this, SLOT(resultPost(TaskSendPost*,QByteArray,QString)));
		taskhub()->queueTask(task);
	}

	void WakabaAgent::createThread(const PostDescriptor& post, const QString& captcha)
	{
		TaskPtr task = TaskPtr(new WakabaTaskSendPost(post, captcha, requestManager(), "Creating thread", this));
		m_postRequests[task->id()] = post.parent_thread;

		connect(task.data(), SIGNAL(result(TaskSendPost*,QByteArray,QString)), this, SLOT(resultPost(TaskSendPost*,QByteArray,QString)));
		taskhub()->queueTask(task);
	}

	void WakabaAgent::deletePost(const PostId& postId, const QString& password)
	{

	}

	void WakabaAgent::deleteThread(const ThreadId& threadId, const QString& password)
	{

	}

	void WakabaAgent::resultPost(TaskSendPost* task, const QByteArray& data, const QString& contentType)
	{
		auto it = m_postRequests.find(task->id());
		if(it == m_postRequests.end())
			return;
		ThreadId threadId = it->second;
		m_postRequests.erase(it);
		QString regexp = QString::fromUtf8("<h1 style=\"text-align: center\">\\w+: ([^<>]*)");
		QRegExp rx(regexp);
		if(rx.indexIn(QString::fromUtf8(data)) != -1)
		{
			log(llDebug, "Error: %s", qPrintable(rx.cap(1)));
			emit postingError(rx.cap(1));
			return;
		}
		else
		{
			//emit postingError("");
			// TODO we can emit here 'thread' slot as well
		}
		log(llDebug, "board: %s, thread: %d", qPrintable(threadId.boardId()), threadId.threadNumericId());
		emit postingDone(threadId);
		//emit refresh(m_board, m_threadID, true);
	}

	QStringList WakabaAgent::ratings()
	{
		return QStringList();
	}

	ChanAgent* WakabaAgent::clone(const QString& host)
	{
		return new WakabaAgent(host);
	}

	QString WakabaAgent::id()
	{
		return "wakaba";
	}
}

Q_EXPORT_PLUGIN2(wakabaagent, Wishmaster::WakabaAgent)

